package gold.contest;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC5227 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maximumTop(int[] nums, int k) {
            if (nums.length <= 0) return -1;
            if (nums.length <= 1) return k % 2 == 0 ? nums[0] : -1;

            int curStackPeek = -1, i = 0;
            while (k-- > 0 && i < nums.length) {
                if (nums[i] > curStackPeek && k >= 1) {
                    curStackPeek = nums[i];
                }
                ++i;
            }

            if (i < nums.length && nums[i] > curStackPeek) return nums[i];
            return curStackPeek;
        }
    }


    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[5,2,2,4,0,6]"), 4));
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[2]"), 1));
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[31,15,92,84,19,92,55]"), 4));
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[91,98,17,79,15,55,47,86,4,5,17,79,68,60,60,31,72,85,25,77,8,78,40,96,76,69,95,2,42,87,48,72,45,25,40,60,21,91,32,79,2,87,80,97,82,94,69,43,18,19,21,36,44,81,99]"), 2));
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[35,43,23,86,23,45,84,2,18,83,79,28,54,81,12,94,14,0,0,29,94,12,13,1,48,85,22,95,24,5,73,10,96,97,72,41,52,1,91,3,20,22,41,98,70,20,52,48,91,84,16,30,27,35,69,33,67,18,4,53,86,78,26,83,13,96,29,15,34,80,16,49]"), 15));
        System.out.println(solution.maximumTop(InputUtil.toIntegerArray("[68,76,53,73,85,87,58,24,48,59,38,80,38,65,90,38,45,22,3,28,11]"), 1));
    }

    public static void main(String[] args) throws Exception {
        LC5227 an = new LC5227();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
